package com.kevin.Code.Array;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

/**
 * @author Vinlee Xiao
 * @Classname FindAllNumbersDisappearedinanArray
 * @Description Leetcode 448. 找到所有数组中消失的数字 简单难度 hot100
 * @Date 2021/11/21 19:20
 * @Version 1.0
 */
public class FindAllNumbersDisappearedinanArray {


    /**
     * Hashmap的使用
     *
     * @param nums
     * @return
     */
    public List<Integer> findDisappearedNumbers(int[] nums) {

        int len = nums.length;
        List<Integer> result = new ArrayList<>();
        HashSet<Integer> hashSet = new HashSet<>();

        //
        for (int num : nums) {
            hashSet.add(num);
        }
        for (int i = 1; i <= len; i++) {
            if (!hashSet.contains(i)) {
                result.add(i);
            }

        }


        return result;
    }


    public List<Integer> findDisappearedNumbers1(int[] nums) {

        int len = nums.length;
        List<Integer> result = new ArrayList<>();

        for (int i = 0; i < len; i++) {
            int index = (nums[i] - 1) % len;
            nums[index] = (nums[i] + len);
        }


        for (int i = 0; i < len; i++) {
            if (nums[i] <= len) {
                result.add(i + 1);
            }
        }

        return result;
    }
}
